package com.cq.androidtools.recycleview

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cq.androidtools.R
import com.cq.androidtools.recycleview.base.BaseViewHolder
import com.cq.androidtools.recycleview.base.OnItemClickListener
import com.cq.androidtools.recycleview.table.GridItemDecoration
import com.cq.androidtools.recycleview.table.GridLayoutManagerU
import com.cq.base.recycleview.base.BaseRecycleViewAdapter


/**
 * 作者: CQ
 * 日期: 2021-03-16
 * 说明: 实例
 */
class RecycleviewActivity : AppCompatActivity() {
    private lateinit var rv_user: RecyclerView
    private lateinit var adapter: ARecycleAdapter
    private lateinit var context: Context
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    private fun initView() {
        context = this
        rv_user = findViewById<View>(R.id.rv_user) as RecyclerView
//        rv_user.setItemViewCacheSize(200)
        val griLayoutManager = GridLayoutManagerU(this, 100)

//        griLayoutManager.isItemPrefetchEnabled = true
        griLayoutManager.initialPrefetchItemCount = 1000
        griLayoutManager.orientation = GridLayoutManager.HORIZONTAL
        rv_user.setItemViewCacheSize(1000)
        rv_user.setHasFixedSize(true)
        rv_user.isNestedScrollingEnabled = false
        rv_user.layoutManager = griLayoutManager
        rv_user.addItemDecoration(GridItemDecoration(this))

        val list = mutableListOf<String>()
        for (i in 0..999){
            list.add(i.toString())
        }
        val exampleAdapter = ExampleAdapter(list, this)
        rv_user.adapter = exampleAdapter
        exampleAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClickListener(v: View?, position: Int) {
                Log.e("--", "pos = $position")
            }
        })


    }


}

class ExampleAdapter(list: MutableList<String>, context: Context) : BaseRecycleViewAdapter<String>(context,list) {

    override fun getInflate(): Int = R.layout.item_text

    override fun bindData(holder: BaseViewHolder, data: String, position: Int) {
        holder.getEditText(R.id.item_grid_text).setText(data)
        Log.e("bindData", "text = $data")
    }
}